--[[ Lambda UnitFrames
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local filePath = "Interface\\AddOns\\LambdaUI\\"
local genericFilePath = "Interface\\AddOns\\LambdaUI\\Generic\\"

--[[  Lambda Partyframe Class

--]]
local Lambda = {}

function Lambda:new(UnitId, id)
	local Lambda = {}
    local filePath = filePath.."UnitFrames\\"
	
    Lambda = CreateFrame("Button", "Lambda"..UnitId, UIParent, "SecureUnitButtonTemplate")
   	
	Lambda.UnitId = UnitId
	Lambda.BackgroundTexture = Lambda:CreateTexture("Lambda"..UnitId.."Panel","BORDER")
	Lambda.ClassColorBackgroundTexture = Lambda:CreateTexture("Lambda"..UnitId.."Class","BACKGROUND")
	Lambda.NameString = Lambda:CreateFontString("Lambda"..UnitId.."Name","OVERLAY")
	Lambda.ManaBar = CreateFrame("StatusBar","Lambda"..UnitId.."ManaBar", Lambda)
	Lambda.HealthBar = CreateFrame("StatusBar","Lambda"..UnitId.."HealthBar", Lambda)
	Lambda.OverlayTexture = Lambda:CreateTexture("Lambda"..UnitId.."Overlay","OVERLAY")
	Lambda.HealthValue = Lambda:CreateFontString("Lambda"..UnitId.."HealthValue","OVERLAY")
	
	Lambda:SetWidth(136)
	Lambda:SetHeight(41)
	
	Lambda:SetAttribute("unit",UnitId)
	Lambda:SetAttribute("type2","menu")
	Lambda:SetAttribute("type3","assist")
	
	RegisterUnitWatch(Lambda)
	
	Lambda.OverlayTexture:SetTexture(filePath.."PartyOverlay.tga")
	Lambda.OverlayTexture:SetPoint("TOPLEFT",Lambda)
	Lambda.OverlayTexture:SetWidth(256)
	Lambda.OverlayTexture:SetHeight(64)
	
	Lambda.BackgroundTexture:SetTexture(filePath.."PartyBackground.tga")
	Lambda.BackgroundTexture:SetPoint("TOPLEFT",Lambda)
	Lambda.BackgroundTexture:SetWidth(256)
	Lambda.BackgroundTexture:SetHeight(64)
		
	Lambda.ClassColorBackgroundTexture:SetTexture(filePath.."ClassColorBackground.tga")
	Lambda.ClassColorBackgroundTexture:SetPoint("TOPLEFT",Lambda,"TOPLEFT",1, 0)
	Lambda.ClassColorBackgroundTexture:SetWidth(256)
	Lambda.ClassColorBackgroundTexture:SetHeight(64)
	
	Lambda.NameString:SetFont(genericFilePath.."bold.ttf",10)
	Lambda.NameString:SetText(UnitName(UnitId))
	Lambda.NameString:SetPoint("TOPLEFT",Lambda,"TOPLEFT",22, -11)
	
	Lambda.HealthValue:SetFont(genericFilePath.."bold.ttf",10)
	Lambda.HealthValue:SetPoint("TOPRIGHT",Lambda,"TOPRIGHT",-4,-11)
	
	Lambda.ManaBar:SetStatusBarTexture(filePath.."StatusBar.tga")
	Lambda.ManaBar:SetPoint("TOPLEFT",Lambda,"TOPLEFT",11, -32)
	Lambda.ManaBar:SetWidth(121)
	Lambda.ManaBar:SetHeight(5)
	Lambda.ManaBar:SetStatusBarColor(0, 0.25, 1)
	Lambda.ManaBar:SetMinMaxValues(0, UnitManaMax(UnitId))
	Lambda.ManaBar:SetValue(UnitMana(UnitId))
	Lambda.ManaBar:SetFrameStrata("BACKGROUND")
	
	Lambda.HealthBar:SetStatusBarTexture(filePath.."StatusBar.tga")
	Lambda.HealthBar:SetPoint("TOPLEFT",Lambda,"TOPLEFT",11, -24)
	Lambda.HealthBar:SetWidth(121)
	Lambda.HealthBar:SetHeight(5)
	Lambda.HealthBar:SetStatusBarColor(0.964, 0.631, 0.102)
	Lambda.HealthBar:SetMinMaxValues(0, UnitHealthMax(UnitId))
	Lambda.HealthBar:SetValue(UnitHealth(UnitId))
	Lambda.HealthBar:SetFrameStrata("BACKGROUND")
	
	Lambda.showmenu = function()
				ToggleDropDownMenu(1, nil, getglobal("PartyMemberFrame"..id.."DropDown"), Lambda:GetName(), 0, 0)
	end

	Lambda.BuffFrame = CreateFrame("Frame","Lambda"..UnitId.."Buffs", Lambda)
	for i=0,3 do -- make 4 little buffFrames
		local buffButton = Lambda.BuffFrame:CreateTexture("Lambda"..UnitId.."Buff"..i+1, "OVERLAY")
		buffButton:SetWidth(12)
		buffButton:SetHeight(12)
		buffButton:SetPoint("TOPLEFT",Lambda.BuffFrame,"TOPLEFT",i*12,0)
	end
	Lambda.BuffFrame:SetPoint("TOPLEFT", Lambda,"TOPLEFT",10,4)
	Lambda.BuffFrame:SetWidth(64)
	Lambda.BuffFrame:SetHeight(16)
	
	Lambda.update = function() 
	if UnitExists(Lambda.UnitId) then
		
		if event == "RAID_ROASTER_UPDATE" or event == "PARTY_MEMBERS_CHANGED" or event == "PLAYER_ENTERING_WORLD" then
			Lambda.NameString:SetText(UnitName(Lambda.UnitId))
			
			if UnitClass(Lambda.UnitId) then
				local r = RAID_CLASS_COLORS[select(2,UnitClass(Lambda.UnitId))].r
				local g = RAID_CLASS_COLORS[select(2,UnitClass(Lambda.UnitId))].g
				local b = RAID_CLASS_COLORS[select(2,UnitClass(Lambda.UnitId))].b
		
				Lambda.ClassColorBackgroundTexture:SetVertexColor(r,g,b)
			
			end
		end
		
		if event == "UNIT_HEALTH" and arg1 == Lambda.UnitId then
			local Health = UnitHealth(Lambda.UnitId) - UnitHealthMax(Lambda.UnitId)
			
			if Health == 0 then
				Health = ""
			elseif -Health == UnitHealthMax(Lambda.UnitId) then -- is dead
				Health = "tot"
			end
			Lambda.HealthValue:SetText(Health)
			
			Lambda.HealthBar:SetMinMaxValues(0, UnitHealthMax(Lambda.UnitId))
			Lambda.HealthBar:SetValue(UnitHealth(Lambda.UnitId))
			
		end
		
		if (event == "UNIT_MANA" or event=="UNIT_RAGE" or event == "UNIT_ENERGY" or event == "UNIT_FOCUS") and arg1 == Lambda.UnitId then
			Lambda.ManaBar:SetMinMaxValues(0, UnitManaMax(Lambda.UnitId))
			Lambda.ManaBar:SetValue(UnitMana(Lambda.UnitId))
			
		end
		
		if event == "UNIT_AURA" and arg1 == Lambda.UnitId then
			Lambda.NameString:SetVertexColor(unpack(LambdaUI.TextColors.BorderFont))
			for i=1,15 do
				local debuffType = select(5,UnitDebuff(Lambda.UnitId,i))
					if debuffType then
						Lambda.NameString:SetVertexColor(DebuffTypeColor[debuffType].r,DebuffTypeColor[debuffType].g,DebuffTypeColor[debuffType].b) -- newer Debuffs gets priority!
					end
			end
		end 
		
		if (event == "PLAYER_ENTERING_WORLD" or event == "PARTY_LEADER_CHANGED" or event == "PARTY_LOOT_METHOD_CHANGED") and Lambda.UnitId ~= "pet" then
			local Index = tonumber(select(3, string.find(Lambda.UnitId, "(%d)")))
			if select(2, GetLootMethod()) == Index and not GetPartyLeaderIndex() == Index then -- player is only Masterlooter
				Lambda.ClassColorBackgroundTexture:SetTexture(filePath.."LooterClassColorBackground.tga")
				
			elseif select(2, GetLootMethod()) == Index and GetPartyLeaderIndex() == Index then -- player is Masterlooter AND Leader
				Lambda.ClassColorBackgroundTexture:SetTexture(filePath.."LeaderLootClassColorBackground.tga")
			
			elseif GetPartyLeaderIndex() == Index then -- player only Leader
				Lambda.ClassColorBackgroundTexture:SetTexture(filePath.."LeaderClassColorBackground.tga")
	
			else -- player is nothing just a player
				Lambda.ClassColorBackgroundTexture:SetTexture(filePath.."ClassColorBackground.tga")
	
			end
		end
		
		if ((event == "PLAYER_ENTERING_WORLD" or event == "RAID_ROSTER_UPDATE") and UnitInRaid(Lambda.UnitId) and LambdaUI.Settings.HidePartyInRaid ~= true) and Lambda.UnitId ~= "pet" then -- hide in Raid
			Lambda:Hide()
			UnregisterUnitWatch(Lambda)
			
		end
		if ((event == "PLAYER_ENTERING_WORLD" or event == "RAID_ROSTER_UPDATE") and UnitInRaid(Lambda.UnitId) and LambdaUI.Settings.HidePartyInRaid == true) and Lambda.UnitId ~= "pet" then -- show in Raid
			Lambda:Show()
			RegisterUnitWatch(Lambda)
			
		end
		if (LambdaUI.Settings.HidePartyInRaid ~= true) and Lambda.UnitId ~= "pet" then
			Lambda:Show()
			RegisterUnitWatch(Lambda)
		end
		
		end
		if event == "UNIT_AURA" then
			local buffTextures = {}
			for buff=1,16 do
				buffTextures[buff] = select(3, UnitBuff(UnitId, buff, true))
			end
			for i=0,3 do
				local buffButton = getglobal("Lambda"..UnitId.."Buff"..i+1)
				
				buffButton:SetTexture(buffTextures[i])
			end
		end

	end
	
	Lambda:RegisterEvent("UNIT_MANA")	
	Lambda:RegisterEvent("UNIT_FOCUS")	
	Lambda:RegisterEvent("UNIT_ENERGY")	
	Lambda:RegisterEvent("UNIT_HEALTH")	
	Lambda:RegisterEvent("RAID_ROSTER_UPDATE")
	Lambda:RegisterEvent("UNIT_AURA")
	Lambda:RegisterEvent("UNIT_RAGE")
	Lambda:RegisterEvent("PLAYER_ENTERING_WORLD")
	Lambda:RegisterEvent("PARTY_MEMBERS_CHANGED")
	Lambda:RegisterEvent("PARTY_LEADER_CHANGED")
	Lambda:RegisterEvent("PARTY_LOOT_METHOD_CHANGED")
	
	Lambda.UpdateFontColors = function()
		Lambda.NameString:SetVertexColor(unpack(LambdaUI.TextColors.BorderFont))
		Lambda.HealthValue:SetVertexColor(unpack(LambdaUI.TextColors.BorderFont))
		
	end
	
	RegisterUnitWatch(Lambda)
	
	Lambda:SetScript("OnEvent",Lambda.update)
	
	Lambda:SetScript("OnEnter", function()
		GameTooltip:SetOwner(Lambda,"ANCHOR_PRESERVE")
		GameTooltip:SetUnit(Lambda.UnitId)
	end)
	
	Lambda:SetScript("OnLeave", function()
		GameTooltip:Hide()
	end)
	
	Lambda:RegisterForClicks("AnyDown")	
	table.insert(LambdaObjectTable,Lambda:GetName())
	SecureUnitButton_OnLoad(Lambda, UnitId, Lambda.showmenu);

	return Lambda
end 

--[[ Lambda Big Frame Class

--]]
local FullLambda = {}

function FullLambda:new(UnitId)
	local FullLambda = {}
	local filePath = filePath.."UnitFrames\\"
	
	FullLambda = Lambda:new(UnitId)
	
	FullLambda.BorderTexture = FullLambda:CreateTexture("Lambda"..UnitId.."Border","ARTWORK")
	FullLambda.PortraitTexture = FullLambda:CreateTexture("Lambda"..UnitId.."Portrait","BORDER")
	SetPortraitTexture(FullLambda.PortraitTexture, UnitId)
	
	FullLambda.BackgroundTexture:Hide()
	FullLambda.BackgroundTexture = nil
	FullLambda.BackgroundTexture = FullLambda:CreateTexture("Lambda"..UnitId.."Background","BACKGROUND")
		
	FullLambda.PortraitTexture:SetWidth(62)
	FullLambda.PortraitTexture:SetHeight(62)
	
	FullLambda.PortraitTexture:SetPoint("TOPLEFT",FullLambda,"TOPLEFT",1, -1)
	
	FullLambda.HealthText = FullLambda:CreateFontString("Lambda"..UnitId.."HealthText","OVERLAY")
	
	FullLambda.ManaText = FullLambda:CreateFontString("Lambda"..UnitId.."ManaText","OVERLAY")
	FullLambda.ManaValue = FullLambda:CreateFontString("Lambda"..UnitId.."ManaValue","OVERLAY")
	
	FullLambda.Level = FullLambda:CreateFontString("Lambda"..UnitId.."Level","OVERLAY")
	
	FullLambda:SetWidth(256)
	FullLambda:SetHeight(64)
	
	FullLambda.OverlayTexture:SetTexture(filePath.."PlayerOverlay.tga")
		
	FullLambda.BorderTexture:ClearAllPoints()
	FullLambda.BorderTexture:SetPoint("TOPLEFT",FullLambda,"TOPLEFT",0,0)
	FullLambda.BorderTexture:SetWidth(256)
	FullLambda.BorderTexture:SetHeight(64)
	
	FullLambda.BorderTexture:SetTexture(filePath.."PlayerBorder.tga")
	
	FullLambda.BackgroundTexture:SetTexture(filePath.."PlayerBackground.tga")
	FullLambda.BackgroundTexture:SetPoint("TOPLEFT",FullLambda)
	FullLambda.BackgroundTexture:SetWidth(256)
	FullLambda.BackgroundTexture:SetHeight(64)
	
	FullLambda.ClassColorBackgroundTexture:Hide()
	FullLambda.ClassColorBackgroundTexture = nil
	
	FullLambda.ManaBar:ClearAllPoints()
	FullLambda.ManaBar:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 85, -59)
	FullLambda.ManaBar:SetPoint("TOPRIGHT",FullLambda,"TOPRIGHT", -16, -59)
	FullLambda.ManaBar:SetHeight(2)
	FullLambda.ManaBar:SetStatusBarColor(1,1,1)
	FullLambda.ManaBar:SetFrameStrata("MEDIUM")
	FullLambda.ManaBar:SetStatusBarTexture("Interface\\Buttons\\WHITE8x8")
	
	FullLambda.HealthBar:ClearAllPoints()
	FullLambda.HealthBar:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 85, -47)
	FullLambda.HealthBar:SetPoint("TOPRIGHT",FullLambda,"TOPRIGHT", -16, -47)
	FullLambda.HealthBar:SetHeight(2)
	FullLambda.HealthBar:SetStatusBarColor(1,1,1)
	FullLambda.HealthBar:SetFrameStrata("MEDIUM")
	FullLambda.HealthBar:SetStatusBarTexture("Interface\\Buttons\\WHITE8x8")
	FullLambda.NameString:Hide()
	FullLambda.NameString = nil
   
	FullLambda.ManaText:SetFont(genericFilePath.."bold.ttf", 8)
	FullLambda.ManaText:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 85, -50)
   
	FullLambda.HealthText:SetFont(genericFilePath.."bold.ttf", 8)
	FullLambda.HealthText:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 85, -38)
   
	FullLambda.ManaValue:SetFont(genericFilePath.."bold.ttf", 8)
	FullLambda.ManaValue:SetPoint("TOPRIGHT",FullLambda,"TOPRIGHT", -16 ,-50)
	FullLambda.ManaValue:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 150, -50)
    FullLambda.ManaValue:SetJustifyH("RIGHT")
   
	FullLambda.HealthValue:SetFont(genericFilePath.."bold.ttf", 8)
	FullLambda.HealthValue:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 150, -38)
	FullLambda.HealthValue:SetPoint("TOPRIGHT",FullLambda,"TOPRIGHT", -16 ,-38)
	FullLambda.HealthValue:SetJustifyH("RIGHT")
	
	FullLambda.Level:SetFont(genericFilePath.."bold.ttf", 8)
	FullLambda.Level:SetPoint("TOPLEFT",FullLambda,"TOPLEFT", 64, -25)
    FullLambda.Level:SetPoint("BOTTOMRIGHT",FullLambda,"BOTTOMRIGHT", -178, 24)
    
	FullLambda.showmenu = function()
			ToggleDropDownMenu(1, nil, PlayerFrameDropDown, FullLambda:GetName(), 0, 0);
	end
	SecureUnitButton_OnLoad(FullLambda, "player", FullLambda.showmenu);
	
	FullLambda.HealthText:SetText(LambdaLocale.Health[GetLocale()])
	
	FullLambda.update = function()
		local healthValue, manaValue;
		FullLambda.Level:SetText(UnitLevel(UnitId))
		if ((event == "UNIT_MANA" or event == "UNIT_RAGE" or event == "UNIT_ENERGY" or event == "UNIT_FOCUS") and arg1 == UnitId) or event == "PLAYER_ENTERING_WORLD" then
			manaValue = assert(loadstring("return function(UnitId) return "..LambdaUI.VarTexts["PlayerMana"].." end"))();
			FullLambda.ManaBar:SetMinMaxValues(0, UnitManaMax(UnitId))
			FullLambda.ManaBar:SetValue(UnitMana(UnitId))
			FullLambda.ManaValue:SetText(manaValue(UnitId))
		end
		if (event == "UNIT_HEALTH" and arg1 == UnitId) or event == "PLAYER_ENTERING_WORLD" then
			healthValue = assert(loadstring("return function(UnitId) return "..LambdaUI.VarTexts["PlayerHealth"].." end"))(UnitId);
			FullLambda.HealthBar:SetMinMaxValues(0, UnitHealthMax(UnitId))
			FullLambda.HealthBar:SetValue(UnitHealth(UnitId))
			FullLambda.HealthValue:SetText(healthValue(UnitId))
		end
		if event == "PLAYER_ENTERING_WORLD" or (event == "UNIT_DISPLAYPOWER" and arg1 == UnitId) then
			if UnitPowerType(UnitId) == 0 then -- Mana
				FullLambda.ManaText:SetText(LambdaLocale.Mana[GetLocale()])
			elseif UnitPowerType(UnitId) == 1 then -- Rage
				FullLambda.ManaText:SetText(LambdaLocale.Rage[GetLocale()])
			elseif UnitPowerType(UnitId) == 2 then -- Focus
				FullLambda.ManaText:SetText(LambdaLocale.Focus[GetLocale()])
			elseif UnitPowerType(UnitId) == 3 then -- Energy
				FullLambda.ManaText:SetText(LambdaLocale.Energy[GetLocale()])
			else
				FullLambda.ManaText:SetText("")
			end
		end
		if event == "UNIT_PORTRAIT_UPDATE" then
			SetPortraitTexture(FullLambda.PortraitTexture, UnitId)
		end
	end
	
		FullLambda:RegisterEvent("PLAYER_ENTERING_WORLD")
		FullLambda:RegisterEvent("UNIT_MANA")	
		FullLambda:RegisterEvent("UNIT_RAGE")	
		FullLambda:RegisterEvent("UNIT_FOCUS")	
		FullLambda:RegisterEvent("UNIT_ENERGY")	
		FullLambda:RegisterEvent("UNIT_HEALTH")	
		FullLambda:RegisterEvent("UNIT_PORTRAIT_UPDATE")
		FullLambda:RegisterEvent("UNIT_DISPLAYPOWER")

	FullLambda.UpdateFontColors = function()
		FullLambda.HealthText:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.HealthValue:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.ManaText:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.ManaValue:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.Level:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.ManaBar:SetStatusBarColor(unpack(LambdaUI.TextColors.GlassFont))
		FullLambda.HealthBar:SetStatusBarColor(unpack(LambdaUI.TextColors.GlassFont))
	end
		
		FullLambda:SetScript("OnEvent",FullLambda.update)
		
	RegisterUnitWatch(FullLambda)
	
	return FullLambda
end


local Kappa = {}
function Kappa:new(UnitId)
	local Kappa = {}
	local filePath = filePath.."UnitFrames\\"
	local TextColor = "|cFF000000"
	
	Kappa = FullLambda:new(UnitId)
	Kappa.BorderTexture:SetTexture(filePath.."TargetBorder.tga")
	
	Kappa.BackgroundTexture:Hide()
	Kappa.BackgroundTexture = nil
	Kappa.BackgroundTexture = Kappa:CreateTexture("Lambda"..UnitId.."Background","BACKGROUND")
	Kappa.Combo = Kappa:CreateFontString("Lambda"..UnitId.."Combo","OVERLAY")
	
	Kappa.LuckyCharm = Kappa:CreateTexture("Lambda"..UnitId.."LuckyCharm","OVERLAY")
	
	Kappa.BackgroundTexture:SetTexture(filePath.."TargetBackground.tga")

	Kappa.BackgroundTexture:SetPoint("TOPLEFT",Kappa)
	Kappa.BackgroundTexture:SetWidth(256)
	Kappa.BackgroundTexture:SetHeight(64)
	
	
	Kappa.OverlayTexture:SetPoint("TOPLEFT",Kappa)
	Kappa.OverlayTexture:SetWidth(256)
	Kappa.OverlayTexture:SetHeight(64)
	Kappa.OverlayTexture:SetTexture(filePath.."TargetOverlay.tga")
	
	Kappa.PortraitTexture:ClearAllPoints()
	Kappa.PortraitTexture:SetPoint("TOPLEFT",Kappa,"TOPLEFT", 134, -1)
	Kappa.PortraitTexture:SetHeight(61)
	Kappa:SetAttribute("unit",UnitId)
	Kappa:SetAttribute("*type1","target")
	Kappa:SetAttribute("*type2","menu")
	
	Kappa:SetWidth(192)
	Kappa:SetHeight(64)
	
	Kappa.BorderTexture:ClearAllPoints()
	Kappa.BorderTexture:SetPoint("TOPLEFT",Kappa,"TOPLEFT",0,0)
	Kappa.BorderTexture:SetWidth(256)
	Kappa.BorderTexture:SetHeight(64)
	
	Kappa.Name = Kappa:CreateFontString("Lambda"..UnitId.."Name","OVERLAY")
	Kappa.Name:SetFont(genericFilePath.."bold.ttf", 10)
	
	Kappa.Name:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 2, -27)
	
	Kappa.ManaValue:ClearAllPoints()
	Kappa.ManaValue:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 55, -38)
	Kappa.ManaValue:SetFont(genericFilePath.."bold.ttf", 14)
	
	Kappa.HealthValue:ClearAllPoints()
	Kappa.HealthValue:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 55, -12)
	Kappa.HealthValue:SetFont(genericFilePath.."bold.ttf", 14)
		
	Kappa.ManaText:Hide()
	Kappa.ManaText = nil
	Kappa.HealthText:Hide()
	Kappa.HealthText = nil
	Kappa.Level:Hide()
	Kappa.Level = nil
	
	Kappa.Combo:SetPoint("TOPLEFT",Kappa,"TOPLEFT",192, -6)
	Kappa.Combo:SetFont(genericFilePath.."bold.ttf",14)
	
	Kappa.LuckyCharm:SetWidth(32)
	Kappa.LuckyCharm:SetHeight(32)
	Kappa.LuckyCharm:SetPoint("TOP", Kappa.PortraitTexture)
	Kappa.LuckyCharm:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
	Kappa.showmenu = function()
			ToggleDropDownMenu(1, nil, TargetFrameDropDown, Kappa:GetName(), 0, 0);
	end
	
	Kappa.update = function()
		if (event == "UNIT_HEALTH" and arg1 == UnitId) or event == "PLAYER_TARGET_CHANGED" or event == "RAID_TARGET_UPDATE" then
			Kappa.HealthValue:SetFormattedText("%.0i%%",(UnitHealth(UnitId)/UnitHealthMax(UnitId)*100))
			if GetRaidTargetIndex(UnitId) ~= nil then
				SetRaidTargetIconTexture(Kappa.LuckyCharm, GetRaidTargetIndex(UnitId))
				Kappa.LuckyCharm:Show()
			else
				Kappa.LuckyCharm:Hide()
			end
		end
		if ((event == "UNIT_MANA" or event == "UNIT_ENERGY" or event == "UNIT_FOCUS") and arg1 == UnitId) or event == "PLAYER_TARGET_CHANGED"  then
			Kappa.ManaValue:SetText(UnitMana(UnitId))
		end
		if event == "PLAYER_COMBO_POINTS" or event == "PLAYER_TARGET_CHANGED" then
			Kappa.Combo:SetText(GetComboPoints())
		end
		Kappa.PortraitTexture:SetTexture(nil)
		SetPortraitTexture(Kappa.PortraitTexture,UnitId)
		
		Kappa.BorderTexture:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
		Kappa.BackgroundTexture:SetVertexColor(unpack(LambdaUI.ThemeSettings.Background))
		if UnitName("target") then
			Kappa.Name:SetText(UnitName(UnitId))
		end
		for i=1, 6 do
			getglobal("Lambda"..UnitId.."BuffButton"..i.."Icon"):SetTexture(select(3, UnitDebuff(UnitId, i)))
		end
	end
	
		Kappa:RegisterEvent("UNIT_PORTRAIT_UPDATE")
		Kappa:RegisterEvent("PLAYER_TARGET_CHANGED")
		Kappa:RegisterEvent("UNIT_BUFF")
		Kappa:RegisterEvent("UNIT_AURA")
		Kappa:RegisterEvent("UNIT_HEALTH")
		Kappa:RegisterEvent("UNIT_MANA")
		Kappa:RegisterEvent("UNIT_ENERGY")
		Kappa:RegisterEvent("UNIT_FOCUS")
		Kappa:RegisterEvent("PLAYER_COMBO_POINTS")
		Kappa:RegisterEvent("RAID_TARGET_UPDATE")
		
		Kappa:SetScript("OnEvent",Kappa.update)	
	
	for i=1,6 do 
		local KappaBuff = CreateFrame("Button","Lambda"..UnitId.."BuffButton"..i,Kappa, "TargetDebuffButtonTemplate")
		KappaBuff:SetScript("OnClick", function()
				if UnitName(UnitId) == UnitName("player") then
					CancelPlayerBuff(i)
				end
		end)
		KappaBuff:SetWidth(16)
		KappaBuff:SetHeight(16)
		KappaBuff.id = i
		getglobal("Lambda"..UnitId.."BuffButton"..i.."Border"):Hide()
		getglobal("Lambda"..UnitId.."BuffButton"..i.."Border").Show = function() end
		
		if i == 1 then
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",10, 0)
		elseif i == 2 then
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",28, 0)
		elseif i == 3 then
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",44, 0)
		elseif i == 4 then
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",10, -18)
		elseif i == 5 then
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",28, -18)
		else
			KappaBuff:SetPoint("TOPLEFT",Kappa.PortraitTexture,"TOPRIGHT",44, -18)
		end
	end
	
	RegisterUnitWatch(Kappa)
	SecureUnitButton_OnLoad(Kappa, UnitId, Kappa.showmenu);
	
	Kappa.UpdateFontColors = function()
		Kappa.HealthValue:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		Kappa.ManaValue:SetVertexColor(unpack(LambdaUI.TextColors.GlassFont))
		Kappa.Name:SetVertexColor(unpack(LambdaUI.TextColors.BorderFont))
		Kappa.Combo:SetVertexColor(unpack(LambdaUI.TextColors.BorderFont))
	end
	
	return Kappa
end

local Sigma = {} -- very small Unitframe for Pet and Targets
function Sigma:new(UnitId)
	local Sigma = {}
	
	local filePath = filePath .. "UnitFrames\\"
	
	Sigma = Lambda:new(UnitId)
	
	Sigma.BorderTexture = Sigma:CreateTexture("Lambda"..UnitId.."Border","ARTWORK")
	
	Sigma:SetWidth(256)
	Sigma:SetHeight(32)
	
	Sigma.BorderTexture:SetTexture(filePath.."PetBorder.tga")
	Sigma.BorderTexture:SetPoint("TOPLEFT",Sigma)
	Sigma.BorderTexture:SetWidth(256)
	Sigma.BorderTexture:SetHeight(32)
	
	Sigma.BackgroundTexture:SetTexture(filePath.."PetBackground.tga")
	Sigma.BackgroundTexture:SetHeight(32)
	
	Sigma.NameString:ClearAllPoints()
	Sigma.NameString:SetPoint("TOPLEFT",Sigma,"TOPLEFT", 38, -9)
	Sigma.NameString:SetPoint("BOTTOMRIGHT",Sigma,"RIGHT", -157, -9)
	Sigma.NameString:SetJustifyH("CENTER")
	Sigma.NameString:SetJustifyV("MIDDLE")
	
	Sigma.HealthValue:ClearAllPoints()
	Sigma.HealthValue:SetPoint("TOPLEFT",Sigma,"TOPLEFT", 124, -9)
	Sigma.HealthValue:SetPoint("BOTTOMRIGHT",Sigma,"RIGHT", -101, -9)
	Sigma.HealthValue:SetJustifyH("CENTER")
	Sigma.HealthValue:SetJustifyV("MIDDLE")
	
	Sigma.ClassColorBackgroundTexture:SetTexture(filePath.."PetClassColorBackground.tga")
	Sigma.ClassColorBackgroundTexture:ClearAllPoints()
	Sigma.ClassColorBackgroundTexture:SetWidth(256)
	Sigma.ClassColorBackgroundTexture:SetHeight(32)
	
	Sigma.ClassColorBackgroundTexture:SetPoint("TOPLEFT", Sigma)
	
	Sigma.ManaBar:Hide()
	Sigma.ManaBar = nil
	
	Sigma.HealthBar:Hide()
	Sigma.HealthBar = nil
	
	Sigma.OverlayTexture:ClearAllPoints()
	Sigma.OverlayTexture:SetPoint("TOPLEFT",Sigma)
	Sigma.OverlayTexture:SetTexture(filePath.."PetOverlay.tga")
	
	Sigma.OverlayTexture:SetWidth(256)
	Sigma.OverlayTexture:SetHeight(32)
	
	Sigma.update = function()
		if event == "PLAYER_ENTERING_WORLD" or (event == "UNIT_NAME_UPDATE" and arg1 == UnitId) then
			Sigma.NameString:SetText(UnitName(UnitId))
			
		end
		
		if (event == "PLAYER_ENTERING_WORLD" or ((event == "UNIT_HEALTH" or event == "UNIT_PET") and arg1 == UnitId)) and UnitClass(UnitId) then
			local r = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].r
			local g = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].g
			local b = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].b
		
			Sigma.HealthValue:SetText(UnitHealth(UnitId))
			Sigma.ClassColorBackgroundTexture:SetVertexColor(r,g,b)
			
		end
		
		if event == "PLAYER_ENTERING_WORLD" or event == "UNIT_HAPPINESS" then
			local HappinessColors = {
			 { 0.35, 0.24, 0.07 }, -- unhappy
			 { 0.77, 0.76, 0.19 }, -- content
			 { 0.29, 0.79, 0.19 }, -- happy
			}
			if GetPetHappiness() then
				Sigma.NameString:SetVertexColor(unpack(HappinessColors[select(1,GetPetHappiness())]))
				
			end
			
		end
		
	end
	
	Sigma:RegisterEvent("UNIT_HEALTH")
	Sigma:RegisterEvent("UNIT_NAME_UPDATE")
	Sigma:RegisterEvent("UNIT_PET")
	Sigma:RegisterEvent("UNIT_HAPPINESS")
	Sigma:RegisterEvent("PLAYER_ENTERING_WORLD")
	
	Sigma:SetScript("OnEvent", Sigma.update)
	
	RegisterUnitWatch(Sigma)
	
	return Sigma
end 

local Epsilon = {} -- smaller as small !
function Epsilon:new(UnitId)
	local Epsilon = {}
	local filePath = filePath.."UnitFrames\\"
	Epsilon = Sigma:new(UnitId)
	
	Epsilon:SetWidth(128)
	Epsilon:SetHeight(24)
	
	Epsilon.BackgroundTexture:Hide()
	Epsilon.BackgroundTexture = nil
	
	
	Epsilon.BorderTexture:SetTexture(filePath.."TargetsTargetBorder.tga")
	Epsilon.BorderTexture:SetWidth(128)
	Epsilon.BorderTexture:SetHeight(32)
	
	Epsilon.NameString:ClearAllPoints()
	Epsilon.NameString:SetPoint("TOPLEFT",Epsilon,"TOPLEFT", 28, -4)
	Epsilon.NameString:SetPoint("BOTTOMRIGHT",Epsilon,"BOTTOMRIGHT", -13, 4)

	
	Epsilon.ClassColorBackgroundTexture:SetTexture(filePath.."TargetsTargetClassColorBackground.tga")
	Epsilon.ClassColorBackgroundTexture:ClearAllPoints()
	Epsilon.ClassColorBackgroundTexture:SetPoint("TOPLEFT",Epsilon)
	Epsilon.ClassColorBackgroundTexture:SetWidth(128)
	Epsilon.ClassColorBackgroundTexture:SetHeight(32)
	
	Epsilon.OverlayTexture:SetTexture(filePath.."TargetsTargetOverlay.tga")
	Epsilon.OverlayTexture:ClearAllPoints()
	Epsilon.OverlayTexture:SetPoint("TOPLEFT",Epsilon)
	Epsilon.OverlayTexture:SetWidth(128)
	Epsilon.OverlayTexture:SetHeight(32)
	
	Epsilon.update = function()
		if (event == "PLAYER_ENTERING_WORLD" or event == "UNIT_TARGET") and UnitClass(UnitId) then
			local r = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].r
			local g = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].g
			local b = RAID_CLASS_COLORS[select(2,UnitClass(UnitId))].b
			
			Epsilon.ClassColorBackgroundTexture:SetVertexColor(r,g,b)
			
			Epsilon.NameString:SetText(UnitName(UnitId))
			
		end
		
		if event == "PLAYER_ENTERING_WORLD" or (event == "UNIT_HEALTH" and arg1 == UnitId) then

		Epsilon.HealthValue:SetText(UnitHealth(UnitId))
			
		end

	end
	
	Epsilon:RegisterEvent("UNIT_HEALTH")
	Epsilon:RegisterEvent("UNIT_NAME_UPDATE")
	Epsilon:RegisterEvent("PLAYER_ENTERING_WORLD")
	Epsilon:RegisterEvent("UNIT_TARGET")
	
	Epsilon:SetScript("OnEvent", Epsilon.update)
	
	RegisterUnitWatch(Epsilon)
	
	return Epsilon
end

player = FullLambda:new("player")
player:SetPoint("TOPLEFT",UIParent,"TOPLEFT",18, -15)

PlayerFrame:Hide()
PlayerFrame.Show = function() end

pet = Sigma:new("pet")
pet:SetPoint("TOPLEFT",UIParent,"TOPLEFT", 32, -80)

party1 = Lambda:new("party1", 1)
party1:SetPoint("TOPLEFT",UIParent,"TOPLEFT",52,-114)
party2 = Lambda:new("party2", 2)
party2:SetPoint("TOPLEFT",UIParent,"TOPLEFT",52,-162)
party3 = Lambda:new("party3", 3)
party3:SetPoint("TOPLEFT",UIParent,"TOPLEFT",52,-208)
party4 = Lambda:new("party4", 4)
party4:SetPoint("TOPLEFT",UIParent,"TOPLEFT",52,-256)

target = Kappa:new("target")
target:SetPoint("RIGHT",UIParent,"RIGHT", -73,0)

ComboFrame:Hide() -- we dont need combopoints from the original FrameXML
ComboFrame.Show = function() end

targetstarget = Epsilon:new("playertargettarget")
targetstarget:SetPoint("RIGHT",UIParent,"RIGHT",-93, -50)

TargetFrame:Hide()
TargetFrame.Show = function() end

PartyMemberFrame1:Hide()
PartyMemberFrame1.Show = function() end

PartyMemberFrame2:Hide()
PartyMemberFrame2.Show = function() end

PartyMemberFrame3:Hide()
PartyMemberFrame3.Show = function() end

PartyMemberFrame4:Hide()
PartyMemberFrame4.Show = function() end